package com.nuumoblie.bluetoothtools.ble

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.nuumoblie.bluetoothtools.R
import com.nuumoblie.bluetoothtools.ble.db.entity.BleAdvertiseInfo

class PeripheralAdapter(val mContext: Context) :
    RecyclerView.Adapter<PeripheralAdapter.ViewHolder>() {

    var mData: List<BleAdvertiseInfo>? = null;

    fun setData(data: List<BleAdvertiseInfo>) {
        this.mData = data;
        notifyDataSetChanged();
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PeripheralAdapter.ViewHolder {

        val view = LayoutInflater.from(mContext).inflate(R.layout.peripheral_list_item, parent,false);
        return ViewHolder(view)

    }

    override fun getItemCount(): Int {
        return mData?.size ?: 0;
    }

    override fun onBindViewHolder(holder: PeripheralAdapter.ViewHolder, position: Int) {
        holder.setItemValue(mData?.get(position))
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var mTvId: TextView? = null;
        var mTvValue: TextView? = null;

        init {
            mTvId = itemView.findViewById(R.id.tv_manufacturer_id);
            mTvValue = itemView.findViewById(R.id.tv_manufacturer_value);
        }

        fun setItemValue(bleAdvertiseInfo: BleAdvertiseInfo?) {
            mTvId?.setText(bleAdvertiseInfo?.manufacturerId?.toString());
            mTvValue?.setText(bleAdvertiseInfo?.manufacturerValue)
        }
    }

}